#include <iostream>
#include "matrix.h"

Matrix::Matrix(int m,int n)
{
	matrix = new double[m*n];
	x=m;
	y=n;
	setMatrix(m,n);
}

Matrix::~Matrix()
{
	//delete [] matrix;
}

void Matrix::setMatrix(int m,int n)
{
	for(int i=0;i<m*n;i++)
	{
		matrix[i]=0;
	}
}

void Matrix::setMatrix(int m,int n,double v)
{
	matrix[m*x+n]=v;
}

void Matrix::setMatrix(int p,double v)
{
	matrix[p]=v;
}

void Matrix::setMatrix(double m[])
{
	for(int i=0;i<x*y;i++)
		matrix[i] = m[i];
}

void Matrix::setMatri3X3(double p0,double p1,double p2,double p3,double p4,double p5,double p6,double p7,double p8)
{
	matrix[0] = p0;
	matrix[1] = p1;
	matrix[2] = p2;
	matrix[3] = p3;
	matrix[4] = p4;
	matrix[5] = p5;
	matrix[6] = p6;
	matrix[7] = p7;
	matrix[8] = p8;
}

//double *Matrix::getMatrix()
//{
//	return matrix;
//}

bool Matrix::getSameDim(Matrix m)
{
	bool b=0;

	if(x==m.getX() && y==m.getY())
		b=1;

	return b;
}

Matrix Matrix::getSum(Matrix &m)
{
	Matrix *s;
	

	if(getSameDim(m) == 0)
	{
		s = new Matrix(0,0);
		std::cout << "Can't sum. Dimensions don't match." << std::endl;
	}
	else
	{
		s = new Matrix(x,y);
		for(int i=0;i<x*y;i++)
			s->setMatrix(i,matrix[i]+m.getValue(i));
	}

	return *s;
}

Matrix Matrix::getDiff(Matrix &m)
{
	Matrix *s;
	

	if(getSameDim(m) == 0)
	{
		s = new Matrix(0,0);
		std::cout << "Can't subtract. Dimensions don't match." << std::endl;
	}
	else
	{
		s = new Matrix(x,y);
		for(int i=0;i<x*y;i++)
			s->setMatrix(i,matrix[i]-m.getValue(i));
	}

	return *s;
}

Matrix Matrix::getProdScalar(double scalar)
{
	Matrix *s;
	s = new Matrix(x,y);

	for(int i=0;i<x*y;i++)
		s->setMatrix(i,matrix[i]*scalar);

	return *s;
}

//Matrix Matrix::getProdMatrix(Matrix &m)
//{
//	Matrix *s;
//	double sum;
//	int newX = x,
//		newY = m.getY(),
//		idx;
//	
//	if(y == m.getX())
//	{
//		s = new Matrix(x,m.getY());
//		for(int i=0;i<newX;i++)
//		{
//			for(int j=0;j<newY;j++)
//			{
//				sum = 0;
//				idx = i*newX;
//				for(int k=0;k<newX;k++)
//				{
//					sum += matrix[idx + k] * m.getValue(k * newY + j);
//				}
//				s->setMatrix(idx + j,sum);
//			}
//		}
//	}
//	else
//	{
//		std::cout << "Can't multiply. Dimensions incorrect." << std::endl;
//		s = new Matrix(0,0);
//	}
//
//	return *s;
//}

Matrix Matrix::getProdMatrix(Matrix &m)
{
	Matrix *s;
	double sum;
	int newX = x,
		newY = m.getY(),
		idx;
	
	if(y == m.getX())
	{
		s = new Matrix(newX,newY);
		for(int i=0;i<3;i++)//x
		{
			for(int j=0;j<5;j++)//m.getY()
			{
				sum = 0;
				idx = i*4;
				for(int k=0;k<y;k++)
				{
					sum += matrix[idx + k] * m.getValue(k * 5 + j);//m.getY()
				}
				s->setMatrix(idx + j,sum);
			}
		}
	}
	else
	{
		std::cout << "Can't multiply. Dimensions incorrect." << std::endl;
		s = new Matrix(0,0);
	}

	return *s;
}

Matrix Matrix::getTranspose()
{
	Matrix *s;
	s = new Matrix(x,y);

	return *s;
}